package com.ou.csbs.capstone.tools;

import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.models.shapes.Line;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;

/**
 * This is a simple version of the PolyLineDrawingTool that will receive a start and end position from the user's mouse on a click and drag event. A temporary black line is drawn to indicate to the user
 * how long the final line is going to be. A straight line is drawn between the two positions with the current set of attributes from the Attribute model during finalization.
 * 
 * @author Senior Capstone
 */
public class LineDrawingTool extends MouseAdapter{
	private Line tempLine;
	private Position begin, end;
	private RenderableModel model;
	private BasicShapeAttributes creationAttributes;

	public LineDrawingTool(RenderableModel model){
		this.model = model;
		this.creationAttributes = new BasicShapeAttributes();
		this.creationAttributes.setOutlineWidth(3);
		this.creationAttributes.setDrawInterior(false);
		this.creationAttributes.setOutlineMaterial(Material.BLACK);
	}

	@Override
	public void mousePressed(MouseEvent mouseEvent) {
		if(mouseEvent.getButton() == MouseEvent.BUTTON1){
			begin = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(begin != null){
				tempLine = new Line(this.creationAttributes, this.begin, this.begin);
				tempLine.drawShape(this.model);
			}
		}
		mouseEvent.consume();
	}
	@Override
	public void mouseDragged(MouseEvent mouseEvent){
		if(SwingUtilities.isLeftMouseButton(mouseEvent)){
			Position position = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(position != null){
				if(!tempLine.isDrawn(this.model)){
					tempLine.drawShape(this.model);
				}
				tempLine.updatePoint(1, position);
			}else{
				tempLine.removeShape(this.model);
			}
		}
		mouseEvent.consume();
	}
	@Override
	public void mouseReleased(MouseEvent mouseEvent) {
		if(mouseEvent.getButton() == MouseEvent.BUTTON1){
			end = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(end != null){
				tempLine.removeShape(this.model);
				Line drawnLine = new Line(begin, end);
				drawnLine.drawShape(this.model);
			}else{
				tempLine.removeShape(this.model);
			}
		}
		mouseEvent.consume();

	}	
}
